package com.fly.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fly.common.R;
import com.fly.pojo.*;
import com.fly.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/frTag")
public class FrTagController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private UserService userService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;
    @Autowired
    private TagUserService tagUserService;
    @GetMapping("/Blog")
    public R<Page> getPages(Integer page, int pageSize){
        Page<Blog> pageInfo1 =new Page<>(page,pageSize);
        Page<BlogDto> blogDtoPage =new Page<>();
        LambdaQueryWrapper<Blog> queryWrapper=new LambdaQueryWrapper<Blog>();
        queryWrapper.orderByDesc(Blog::getUpdateTime);

        blogService.page(pageInfo1,queryWrapper);
        BeanUtils.copyProperties(pageInfo1,blogDtoPage,"records");
        List<Blog> records= pageInfo1.getRecords();

        blogDtoPage.setRecords( BlogDto(records));

        return R.success( blogDtoPage);
    }

    @GetMapping("/Tag")
    public R<Page> getPages2(Integer page, int pageSize,int id){
        Page<Blog> pageInfo1 =new Page<>(page,pageSize);
        Page<BlogDto> blogDtoPage =new Page<>();
        LambdaQueryWrapper<Blog> queryWrapper=new LambdaQueryWrapper<Blog>();
        LambdaQueryWrapper<TagAndUerID> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(TagAndUerID::getTagsId,id);
//       tagUserService.list();
        List<TagAndUerID> list = tagUserService.list(queryWrapper1);

        List<Long> list1=new ArrayList<>();
        int i=0;
        while (i< list.size()){
          list1.add(list.get(i).getBlogsId());
            i++;

        }

       queryWrapper.in(Blog::getId,list1);
        queryWrapper.orderByDesc(Blog::getUpdateTime);
        blogService.page(pageInfo1,queryWrapper);
        BeanUtils.copyProperties(pageInfo1,blogDtoPage,"records");
        List<Blog> records= pageInfo1.getRecords();
        blogDtoPage.setRecords( BlogDto(records));



        return R.success( blogDtoPage);
    }
    private List<BlogDto> BlogDto( List<Blog> records ){

        return records.stream().map((items)->{
            BlogDto blogDto=new BlogDto();
            BeanUtils.copyProperties(items,blogDto);
            blogDto.setAvatar(userService.getById(blogDto.getUserId()).getAvatar());
            blogDto.setName(userService.getById(blogDto.getUserId()).getUsername());
            blogDto.setType(typeService.getById(blogDto.getTypeId()).getName());
            blogDto.setTag(tagService.getById(tagUserService.getById(blogDto.getId()).getTagsId()).getName());
            return blogDto;
        }).collect(Collectors.toList());
    }
    @GetMapping("/Tags")
    public R<Page> getTag(Integer page, int pageSize){

        Page<Tag> pageInfo =new Page<>(page,pageSize);
        Page<TagDto> typeCountPage =new Page<>();
        tagService.page(pageInfo);
        BeanUtils.copyProperties(pageInfo,typeCountPage,"records");
        List<Tag> records=pageInfo.getRecords();
        List <TagDto> typeCounts = records.stream().map((item)->{
            TagDto typeCount = new TagDto();
            BeanUtils.copyProperties(item,typeCount);
            LambdaQueryWrapper<TagAndUerID> queryWrapper=new LambdaQueryWrapper<TagAndUerID>();
            queryWrapper.eq(TagAndUerID::getTagsId, typeCount.getId());


            typeCount.setCount(  tagUserService.count(queryWrapper));
            return typeCount;
        }).collect(Collectors.toList());
        typeCountPage.setRecords(typeCounts);
        return R.success(  typeCountPage);
    }
}
